<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">制令单入库</block>
				<view slot="right" @tap="rightClick">凭证抬头</view>
			</cu-custom>
			<!-- 抬头凭证 输入弹窗 -->
			<view class="cu-modal" :class="modalName=='Modal'?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">凭证抬头</view>
						<view class="action">
							<text class="cuIcon-close text-red" @click="close"></text>
						</view>
					</view>
					<view class="padding-xl">
						<form>
							<view class="cu-form-group">
								<view class="title">凭证日期：</view>
								<picker mode="date" :value="myModelData.cerDate" @change="cerDateChange">
									<view class="picker">
										{{myModelData.cerDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="title">过账日期：</view>
								<picker mode="date" :value="myModelData.accountDate" @change="accountDateChange">
									<view class="picker">
										{{myModelData.accountDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="title">类型：</view>
								<input placeholder="工号" style="text-align: right;" name="input"
									v-model="myModelData.affairType" disabled></input>
							</view>
							<view class="cu-form-group">
								<view class="title">描述：</view>
								<textarea value="" placeholder="请输入描述" style="text-align: left;text-indent: 2em;"
									v-model="myModelData.affairContent" />
							</view>
							<view class="padding flex flex-direction">
								<button class="cu-btn lg" @click="onSubmit">确定</button>
							</view>
						</form>
					</view>
				</view>
			</view>
			<!-- 查询条件 -->
			<view class="fixed" :style="[{animationDelay: 0.1 + 's'}]">
				<form>
					<view class="cu-form-group last">
						<image class="icon" src="../../static/saomiao.png" mode=""></image>
						<input auto-focus placeholder="请扫描制令单号" name="input" v-model="myFormData.oddNumber"
							@confirm="inputQuery"></input>
					</view>
				</form>
			</view>
			<!-- 数据列表 -->
			<view class="list">
				<view class="card animation-slide-bottom" v-for="(item,index) in list" :key="index"
					:style="[{animationDelay: (index + 1)*0.1 + 's'}]">
					<view class="top">
						<view class="left">制令单号：<text>{{item.commandbillCode}}</text></view>
						<!-- <view class="right">{{item.workbillCode}}</view> -->
					</view>
					<view class="line"></view>
					<view class="bottom">
						<view class="box">
							<view class="lable">
								生产阶别:
							</view>
							<view class="content">
								{{item.produceGrade}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								线别:
							</view>
							<view class="content">
								{{item.lineType}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								加工面别:
							</view>
							<view class="content remark">
								{{item.processFace}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								机种名称:
							</view>
							<view class="content remark">
								{{item.mechanismName}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								规格:
							</view>
							<view class="content remark">
								{{item.gague}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								预计开工时间:
							</view>
							<view class="content remark">
								{{item.expectBegintime}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								预计完工时间:
							</view>
							<view class="content remark">
								{{item.expectFinishtime}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								计划数量:
							</view>
							<view class="content remark">
								{{item.plantNum}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								未入库数量:
							</view>
							<view class="content remark">
								{{item.uninputNum}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								联板数:
							</view>
							<view class="content remark">
								{{item.linkBoardnum}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								条码拼板数:
							</view>
							<view class="content">
								{{item.barcodeBoardnum}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								备注:
							</view>
							<view v-if="item.notes" class="content" :class="statusList[index]?'unfold':'shrink'"
								@click.stop="showNote(index)">
								{{item.notes}}
							</view>
							<view v-else>
								暂无数据
							</view>
						</view>
						<!-- <view class="box">
							<view class="lable numName" style="background-color: #99CC00;">
								入库数量:
							</view>
							<view class="content numInput">
								<input style="width: 260rpx;" type="text" v-model="preservationList[index].num" value="" placeholder="请输入入库数量" />
							</view>
						</view> -->
					</view>
					<view class="line"></view>
					<view class="btns">
						<view class="btn1" @click="prin(item)">打印</view>
						<view class="btn1" @click="transform(item)">转工单</view>
						<!-- <view class="btn2" @click="FirstArticleIssue(index,item)">入库</view> -->
						<view class="btn3" @click="scanShipment(index,item)">扫描入库</view>
					</view>
				</view>
			</view>
			<!-- 打印 输入弹窗 -->
			<view class="cu-modal" :class="modalName1=='Modal1'?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">打印</view>
						<view class="action" @tap="hideModal1">
							<text class="cuIcon-close text-red"></text>
						</view>
					</view>
					<view class="padding-xl">
						<form>
							<view class="cu-form-group">
								<view class="titles">批次号：</view>
								<input placeholder="请输入(扫描)批次号" style="text-align: right;" name="input" type="text"
									v-model="myModelData1.batchNum"></input>
							</view>
							<view class="cu-form-group">
								<view class="titles">批次数量：</view>
								<input placeholder="请输入批次数量" style="text-align: right;" name="input" type="number"
									v-model="myModelData1.batch"></input>
							</view>
							<view class="cu-form-group">
								<view class="titles">打印张数：</view>
								<input placeholder="请输入打印张数" style="text-align: right" type="number"
									v-model="myModelData1.printNum"></input>
							</view>
							<view class="padding flex flex-direction">
								<button class="cu-btn lg" @click="onSubmitPrin">确定</button>
							</view>
						</form>
					</view>
				</view>
			</view>
			<view class="none" v-if="noDataAvailable">
				<view class="none1">
					暂无数据
				</view>
			</view>
			<view class="none2" v-if="noMoreDataAvailable">
				没有更多数据
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		dateFormat
	} from '@/common/util/tools.js'
	export default {
		name: 'instructionReceipt',
		data() {
			return {
				myFormData: {
					oddNumber: ''
				},
				list: [],
				isCard: true,
				pageNo: 1,
				noDataAvailable: false,
				noMoreDataAvailable: false,
				modalName: '',
				myModelData: {
					cerDate: '', //凭证日期
					accountDate: '', // 过账日期
					affairType: '',
					affairContent: ''
				},
				modalName1: '',
				myModelData1: {
					batch: '', // 批次数量
					commandId: '',
					printNum: '', // 打印张数
					mobileType: '入库打印',
					batchNum: ''
				},
				// 有一个子订单  次数数组就有几个对象 用于存储id + 发货数量
				preservationList: [],
				statusList: [], // 备注伸展状态
			}
		},
		onShow(e) {
			this.pageNo = 1
			this.list = []
			this.getMesCommandbillInfoList()
			uni.setStorageSync('instructionReceiptRise', null) // 清空凭证抬头
			let date = new Date()
			this.myModelData.cerDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.accountDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.affairType = uni.getStorageSync('affairType');
		},
		// onLoad(e) {
		// 	// this.getMesCommandbillInfoList();

		// },
		onPullDownRefresh: function() {
			let _this = this
			_this.pageNo = 1
			_this.list = []
			setTimeout(function() {
				uni.stopPullDownRefresh();
				_this.getMesCommandbillInfoList()
			}, 1000);
		},
		onReachBottom: function() {
			let _this = this
			if (this.noDataAvailable == true || this.noMoreDataAvailable == true) {
				return
			}
			_this.pageNo++
			_this.getMesCommandbillInfoList()
		},
		methods: {
			// 制造中心-制令单信息-分页列表查询
			getMesCommandbillInfoList() {
				uni.showLoading({
					mask: true,
					title: '加载中.....'
				})
				this.noDataAvailable = false
				this.noMoreDataAvailable = false
				let data = {
					pageNo: this.pageNo,
					pageSize: 5,
					commandbillCode: this.myFormData.oddNumber,
					controlType: '正常',
					inputState: "未完成",
					// deliveryState: '已完成',
					// glazeState: '已完成',
					// inputState: '未完成',
					// checkState: '已完成'
				}
				let _this = this
				this.$http.get('produce/mesCommandbillInfo/list', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						_this.list = _this.list.concat(res.data.result.records);
						_this.statusList = []
						for (let i = 0; i < _this.list.length; i++) {
							_this.statusList.push(false)
						}
						_this.preservationList = []
						for (let i = 0; i < _this.list.length; i++) {
							let data = {
								id: _this.list[i].id,
								num: ''
							}
							_this.preservationList.push(data)
						}
						if (_this.list.length == 0) {
							_this.noDataAvailable = true;
						} else if (_this.list.length > 0 && res.data.result.records.length == 0) {
							_this.noMoreDataAvailable = true;
						} else if (res.data.result.total <= _this.list.length) {
							_this.noMoreDataAvailable = true;
						}
						console.log(_this.list)
					} else {
						// _this.$tip.error(res.data.message)
						_this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			// 输入框查询
			inputQuery() {
				console.log(this.myFormData.oddNumber)
				this.pageNo = 1;
				this.list = [];
				this.getMesCommandbillInfoList();
				this.myFormData.oddNumber = ''
			},
			// 凭证抬头 按钮
			rightClick() {
				this.modalName = 'Modal'
			},
			// 关闭弹窗
			close() {
				this.modalName = '';
				this.myModelData.affairContent = '';
			},
			// 凭证日期
			cerDateChange(e) {
				console.log(e)
				this.myModelData.cerDate = e.detail.value
			},
			accountDateChange(e) {
				this.myModelData.accountDate = e.detail.value
			},
			onSubmit() {
				uni.showLoading({
					mask: true,
					title: '添加凭证抬头中.....'
				})
				if (this.myModelData.affairContent.replace(/\s+/g, "") == '') {
					// this.$tip.error('描述不能为空');
					this.modal.show({
						type: 'error',
						content: '描述不能为空'
					})
					uni.hideLoading()
					return false
				}
				this.$http.post('storage/mesCertificatePerk/add', this.myModelData).then(res => {
					console.log(res)
					uni.hideLoading()
					if (res.data.success) {
						uni.setStorageSync('instructionReceiptRise', res.data.result.id);
						// this.$tip.success(res.data.message)
						this.modal.show({
							type: 'success',
							content: res.data.message
						})
						this.modalName = '';
						this.myModelData.affairContent = '';
					} else {
						// this.$tip.error(res.data.message)
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			// 入库
			FirstArticleIssue(index, item) {
				if (this.preservationList[index].num == '' || this.preservationList[index].num == 0) {
					// this.$tip.error('入库数量不能为空')
					this.modal.show({
						type: 'error',
						content: '入库数量不能为空'
					})
					return false
				} else if (!uni.getStorageSync('instructionReceiptRise')) {
					let date = new Date()
					this.myModelData.cerDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.accountDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.affairType = uni.getStorageSync('affairType');
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '制令单入库',
					content: '确认要入库吗?',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask: true,
								title: '入库中.....'
							})
							let data = {
								commandId: item.id,
								mobileCode: 262,
								mobileType: '制令单入库',
								perkId: uni.getStorageSync('instructionReceiptRise'),
								reserveCode: this.preservationList[index].produceId,
								inputNum: this.preservationList[index].num,
								reserveCode: item.produceId
							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/addEnterItem', data).then(res => {
								uni.hideLoading()
								if (res.data.success) {
									// this.$tip.success(res.data.message)
									this.modal.show({
										type: 'success',
										content: res.data.message
									})
									this.pageNo = 1
									this.list = []
									this.getMesCommandbillInfoList()
									uni.setStorageSync('instructionReceiptRise', null) // 情况凭证抬头
								} else {
									// this.$tip.error(res.data.message)
									this.modal.show({
										type: 'error',
										content: res.data.message
									})
								}
							})
						}
					}
				})
			},
			// 扫描入库
			scanShipment(index, item) {
				if (!uni.getStorageSync('instructionReceiptRise')) {
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '制令单扫描入库',
					content: '确认要扫描入库吗?',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask: true,
								title: '入库中.....'
							})
							let data = {
								mobileCode: 201,
								mobileType: '制令单扫描入库',
								perkId: uni.getStorageSync('instructionReceiptRise'),
								reserveCode: this.preservationList[index].produceId,
								inputNum: this.preservationList[index].num,
								commandId: item.id
							}
							// this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
							this.$http.post('storage/mesCertificatePerk/scanEnterWarehouse', data).then(
								res => {
									uni.hideLoading()
									if (res.data.success) {
										uni.setStorageSync('instructionReceiptId', res.data.result.id)
										uni.setStorageSync('instructionReceiptItemId', item.id)
										this.$Router.push({
											name: 'instructionReceiptSaoMiao'
										})
									} else {
										// this.$tip.error(res.data.message)
										this.modal.show({
											type: 'error',
											content: res.data.message
										})
									}
								})
						}
					}
				})
			},
			transform(item) {
				this.$http.get('produce/mesCommandbillInfo/endStorage', {
					params: {
						id: item.id
					}
				}).then(res => {
					if (res.data.success) {
						this.modal.show({
							type: 'success',
							content: res.data.message
						})
						this.pageNo = 1
						this.list = []
						this.getMesCommandbillInfoList()
					} else {
						this.modal.show({
							type: 'error',
							content: res.data.message
						})
					}
				})
			},
			// 打印
			prin(item) {
				console.log(item)
				this.myModelData1.commandId = item.id;
				this.myModelData1.batchNum=dateFormat(new Date(), 'YYYYMMDDHHmmss')
				this.myModelData1.printNum = 1
				this.modalName1 = 'Modal1'
			},
			hideModal1() {
				this.modalName1 = ''
			},
			onSubmitPrin() {
				if (this.myModelData1.batch.replace(/\s+/g, "") == '') {
					// this.$tip.error('批次数量不能为空');
					this.modal.show({
						type: 'error',
						content: '批次数量不能为空'
					})
					return false
				} else if (this.myModelData1.printNum.toString().replace(/\s+/g, "") == '') {
					// this.$tip.error('打印张数不能为空');
					this.modal.show({
						type: 'error',
						content: '打印张数不能为空'
					})
					return false
				}
				uni.showModal({
					title: '提示',
					content: '是否打印' + this.myModelData1.printNum + '张？',
					cancelText: '取消',
					confirmText: '打印',
					success: res => {
						if (res.confirm) {
							this.$http.get('storage/mesStorageWholesale/printAdd', {
								params: this.myModelData1
							}).then(res => {
								this.myModelData1.batch = '';
								this.myModelData1.commandId = '';
								this.myModelData1.printNum = '';
								this.myModelData1.batchNum = '';
								if (res.data.success) {
									// this.$tip.success(res.data.message)
									this.modal.show({
										type: 'success',
										content: res.data.message
									})
									this.modalName1 = ''
								} else {
									// this.$tip.error(res.data.message)
									this.modal.show({
										type: 'error',
										content: res.data.message
									})
								}
							})
						}
					}
				})
			},
			// 备注展开
			showNote(index) {
				this.$set(this.statusList, index, !this.statusList[index]);
				console.log(this.statusList[index])
			}
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f1f1f1;

		.fixed {
			position: fixed;
			z-index: 1;
			width: 100%;
			background-color: white;
			height: 100rpx;
			border-bottom: 1rpx solid #eee;

			.last {
				border-bottom: 1rpx solid #eee;
				position: relative;

				.icon {
					width: 30rpx;
					height: 29rpx;
					position: absolute;
					top: 36rpx;
					left: 60rpx;
				}

				input {
					width: 686rpx;
					height: 68rpx;
					background: #FFFFFF;
					border: 1px solid #D5D8DF;
					border-radius: 34px;
					padding-left: 79rpx;
				}
			}
		}

		.list {
			margin-top: 120rpx;

			.card {
				margin: 30rpx;
				box-shadow: 0px 0px 5rpx 2rpx rgba(0, 0, 0, 0.1);
				border-radius: 10rpx;
				background-color: white;

				.top {
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-weight: bold;
					padding: 20rpx 30rpx;

					.left {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #485A75;
						line-height: 50rpx;
					}

					.right {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #191919;
						line-height: 50rpx;
					}
				}

				.line {
					width: 750rpx;
					height: 10rpx;
					background: #F3F5F9;
				}

				.bottom {
					padding: 20rpx 30rpx;

					.box {
						display: flex;
						justify-content: space-between;
						padding: 20rpx 0;
						align-items: center;

						.lable {
							color: #485A75;
							font-size: 28rpx;
						}

						.content {
							width: 70%;
							text-align: right;
							color: #191919;
							font-weight: bold;
							word-break: break-all;
						}

						.shrink {
							height: 40rpx;
							overflow: hidden;
							white-space: nowrap;
							/*不换行*/
							text-overflow: ellipsis;
							/*超出部分文字以...显示*/
							font-weight: 400;
						}

						.unfold {
							text-align: left;
							font-weight: 400;
						}
					}
				}

				.btns {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 0 20rpx;
					font-size: 25rpx;
					padding: 20rpx 30rpx;

					view {
						width: 180rpx;
						height: 64rpx;
						line-height: 50rpx;
						color: white;
						border-radius: 30rpx;
						padding: 10rpx 20rpx;
						text-align: center;
					}

					.btn1 {
						background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
					}

					.btn2 {
						background-image: radial-gradient(circle, #716BA9, #362D85, #57509A);
					}

					.btn3 {
						background-image: radial-gradient(circle, #354D9E, #354D9E, #697BB7);
					}
				}

				.numName {
					font-weight: bold;
					padding: 10rpx;
					border-radius: 10rpx;
					color: white !important;
					width: 150rpx !important;
				}

				input {
					border: 2rpx solid #F0F0F0;
					height: 60rpx;
					padding: 10rpx;
					border-radius: 10rpx;
					font-size: 20rpx;
					margin-left: 39%;
				}
			}
		}
	}

	.none .none1 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}

	.none2 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}

	.cu-btn {
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
		color: white;
	}
</style>
